#include "common.h"
#include "mblur.h"
#include "dof.h"

layout(location = TEXCOORD0) in vec2 tc0;

layout(location = COLOR0) out vec4 oColor;

//////////////////////////////////////////////////////////////////////////////////////////
uniform sampler2D	s_distort;
uniform half4 		e_barrier;	// x=norm(.8f), y=depth(.1f), z=clr
uniform half4 		e_weights;	// x=norm, y=depth, z=clr
uniform half4 		e_kernel;	// x=norm, y=depth, z=clr
//#define	EPSDEPTH	0.001
//////////////////////////////////////////////////////////////////////////////////////////
// Pixel
void main()
{
#ifdef 	USE_DISTORT
  	half 	depth 	= tex2D 	(s_position, 	tc0).z;
	half4 	distort	= tex2D		(s_distort, 	tc0);
	half2	offset	= (distort.xy-(127.0f/255.0f))*def_distort;  // fix newtral offset
	float2	center	= tc0 + offset;
	half 	depth_x	= tex2D 	(s_position, center).z	;
	if ((depth_x+EPSDEPTH)<depth)	center	= tc0;	// discard new sample
#else
	float2	center 	= tc0;
#endif
   	half3 	img 	= tex2D		(s_image, center).rgb;

	// TODO: Fix the implicit cast here.
	//half3	img		= dof		(center);
	half4 	bloom	= tex2D		(s_bloom, center);
	
			img 	= mblur		(center,tex2D(s_position,tc0).rgb,img.rgb);

#ifdef 	USE_DISTORT
 	half3	blurred	= bloom.rgb*def_hdr	;
			img		= lerp	(img,blurred,distort.z);
#endif

 	oColor			= combine_bloom( img, bloom );
}
